﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using Microsoft.SharePoint.Taxonomy;

namespace MS.InformationManagement.BLL.DocumentPrinter
{
    public class UpdateDeliveryDataRadicadoCommand : IBaseCommand
    {

        #region fields

        string _libraryName;
        int _documentId;
        string _phone;
        string _address;
        string _email;
        TaxonomyFieldValue _city;

        #endregion

        #region Constructors

        public UpdateDeliveryDataRadicadoCommand(string libraryName, int documentId, string email, string phone, string address, TaxonomyFieldValue city)
        {
            _libraryName = libraryName;
            _documentId = documentId;     
            _phone = phone;
            _address = address;
            _email = email;
            _city = city;
        }

        #endregion

        public void Execute()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentSite.AllowUnsafeUpdates = true;
                        currentWeb.AllowUnsafeUpdates = true;
                        SPListItem currentDocument = currentWeb.Lists[_libraryName].GetItemById(_documentId);
                        currentDocument[CustomSiteColumns.FIELD_DOCUMENT_PHONE] = _phone;
                        currentDocument[CustomSiteColumns.FIELD_DOCUMENT_ADDRESS] = _address;
                        currentDocument[CustomSiteColumns.FIELD_DOCUMENT_EMAIL] = _email;
                        currentDocument[CustomSiteColumns.FIELD_DOCUMENT_COUNTRY] = _city;
                        currentDocument.Update();
                        currentSite.AllowUnsafeUpdates = false;
                        currentWeb.AllowUnsafeUpdates = false;
                    }
                }
            });     
        }

        public void Undo()
        {
            throw new NotImplementedException();
        }
    }
}
